<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Controls sql operations over the roles table of our database
 *
 *  @author Daniel Torres
 *  @package Musashi
 */

// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access. Sorry');

/**
 *  Table definition. Reflects the Venue table
 */
class TableRoles extends JTable
{
    /**
     *  Parameters handled by the actual table
     */
    var $id             = null;
    var $name           = null;
    var $description    = null;
    
                            
    /**
     *  Constructor
     *  All the Musashi table constructors will be similar, except for the table name
     */
    function __construct( &$db )
    {
        parent::__construct('#__Musashi_Role', 'id', $db);
    }
    
    /**
     *  Counts the number of equipment pieces using this classification
     */
    function getAffectedQueries( $oid )
    {
        // Delete information
        $deleteQuery = "SELECT #__Musashi_Role.id\n"
                    . "FROM #__Musashi_Role INNER JOIN #__Musashi_Member_Show\n"
                    . "ON #__Musashi_Member_Show.idRole = #__Musashi_Role.id\n"
                    . "WHERE #__Musashi_Role.id=".$oid;

        $db   =& JFactory::getDBO();        
        $db->setQuery( $deleteQuery );
        $db->query();
        
        return $db->getNumRows();        
    }
    
    /**
     *  Removes a record from this table. Validates to make sure the operation
     *  is a valid one. Otherwise returns false
     */
    function delete( $oid = null )
    {
        // Make sure we can delete this record
        $affected = $this->getAffectedQueries($oid);
        
        if( $affected == 0 ){
            return parent::delete($oid);
        } else {
            $this->setError("Failed to delete role: There are ".$affected." show entries using it!");
        }
        
        return false;
        
    }
}

?>